﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Read.Api.Application.Command;
using B.S.BaseData.Read.Api.Dtos;
using MediatR;

namespace B.S.BaseData.Read.Api.Application.CommandHander
{
    public class GetMainCommandHander : IRequestHandler<GetMainCommand, ApiPaging<List<GetMainDto>>>
    {

        private readonly IMapper mapper;
        private readonly ILogger<GetMainCommandHander> logger;
        private readonly IBaseRequsitory<Main> mainRequsitory;
        private readonly IBaseRequsitory<User> baseRequsitory;
        private readonly IBaseRequsitory<UserRole> userroleRequsitory;
        private readonly IBaseRequsitory<Role> roleRequsitory;

        public GetMainCommandHander(IMapper mapper, ILogger<GetMainCommandHander> logger, IBaseRequsitory<Main> mainRequsitory, IBaseRequsitory<User> baseRequsitory, IBaseRequsitory<UserRole> userroleRequsitory, IBaseRequsitory<Role> roleRequsitory)
        {
            this.mapper = mapper;
            this.logger = logger;
            this.mainRequsitory = mainRequsitory;
            this.baseRequsitory = baseRequsitory;
            this.userroleRequsitory = userroleRequsitory;
            this.roleRequsitory = roleRequsitory;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<List<GetMainDto>>> Handle(GetMainCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<GetMainDto>> result = new ApiPaging<List<GetMainDto>>();

            var main = mainRequsitory.GetAllAsync();
            var user = baseRequsitory.GetAllAsync();
            var userrole = userroleRequsitory.GetAllAsync();

            var list =
                from a in main
                join u in user on a.UserId equals u.UserId

                where a.IsDelete == false
                select new GetMainDto
                {
                    MainId = a.MainId,
                    Name = a.Name,
                    Sex = a.Sex,
                    Address = a.Address,
                    UserId = a.UserId,
                    CreateTime = a.CreateTime,
                    IsDelete = a.IsDelete,
                    UserName = u.UserName,
                    Password = u.UserName,
                    UserNickName = u.UserNickName,
                    MarketName = a.MarketName,
                    EconomizeName = a.EconomizeName,
                    CityName = a.CityName,
                };

            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();

            result.TotalCount = list.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            var entity = list.OrderBy(x => x.MainId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = mapper.Map<List<GetMainDto>>(entity);

            return Task.FromResult(result);
        }
    }
}
